var express = require("express");
var router = express.Router();
var { UserModel, MovieModel } = require("../mongo/model");
var { aesDecrypt } = require("../utils/crypto");

// res.render 呈现视图
// res.json 发送一个json 数据
// res.send 发送一个字符串
router.get("/", function (req, res, next) {
  console.log(req.session);
  res.render("index", {
    title: "wh2209 - daydayup",
    msg: "hello node",
    flag: !!0,
    text: "<h1>百分号减号语法可以解析标签</h1>",
    html: "<h1>百分号等号语法不可以解析标签</h1>",
    username: req.session.username,
    list: ["vue", "vue3", "node", "react", "小程序"],
  });
});
router.get("/login", function (req, res, next) {
  var username = req.query.username ? aesDecrypt(req.query.username) : "";
  res.render("login", { username: username });
});
router.get("/register", function (req, res, next) {
  res.render("register");
});
router.get("/mine", function (req, res, next) {
  res.render("mine");
});
router.get("/home", (req, res) => {
  if (req.session.username) {
    MovieModel.find({}, {})
      .then((result) => {
        res.render("home", {
          result,
        });
      })
      .catch((err) => {
        res.send(
          `<script>alert('服务器错误，请重新再试');location.reload()</script>`
        );
      });
  } else {
    res.send(
      `<script>alert('session已经失效，请重新登陆');location.href='/login'</script>`
    );
  }
});
router.get("/detail", (req, res) => {
  if (req.session.username) {
    var id = req.query.mid;
    MovieModel.findOne({ id: id }, {})
      .then(result => {
        res.render("detail", {
          result,
        });
      })
      .catch((err) => {
        res.send(
          `<script>alert('服务器错误，请重新再试');location.reload()</script>`
        );
      });
  } else {
    res.send(
      `<script>alert('session已经失效，请重新登陆');location.href='/login'</script>`
    );
  }
});
router.get("/test", (req, res, next) => {
  res.send("<h2>test - test - 测试API接口</h2>");
});
router.get("/logout", (req, res) => {
  req.session.destroy(() => {
    //退出登录，销毁session
    res.redirect("/");
  });
});
router.get("/data", (req, res) => {
  res.json({
    code: 200,
    msg: "查询成功",
    result: { res: "这是我返回给你的信息" },
  });
});
// 可以是get请求，也可以是post请求
router.all("/list", (req, res) => {
  res.json({
    code: 200,
    msg: "list - list  - list",
    result: ["vue", "node", "react"],
  });
});
router.all("/args/:address?/:who?", (req, res) => {
  res.json({
    code: 200,
    msg: "req参数",
    headers: req.headers,
    query: req.query, //查询参数，通过？键名=键值传值
    params: req.params, //参数，不加问号必传，加问号可传可不传
    body: req.body, //post请求的参数
    url: req.url,
    path: req.path,
  });
});
router.all("/userlist", (req, res) => {
  UserModel.find({}, {})
    .then((result) => {
      res.json({
        code: 200,
        msg: "获取用户成功",
        result,
      });
    })
    .catch((err) => {
      res.json({
        code: 500,
        msg: "服务器异常",
        err,
      });
    });
});
router.all("/movie", (req, res) => {
  var limit = req.query.limit || 0;
  MovieModel.find({}, {})
    .limit(limit)
    .then((result) => {
      res.json({
        code: 200,
        msg: "获取电影信息成功",
        result,
      });
    })
    .catch((err) => {
      res.json({
        code: 500,
        msg: "服务器异常",
        err,
      });
    });
});

module.exports = router;
